<template>
	<view class="mainpadding">
		<textarea class="textarea ffffff radius" placeholder="请输入备注内容" v-model="name"></textarea>
		<view class="mainpadding ffffff radius margin_top">
			<view class="titletext xiaohei">请上传图片</view>
			<view class="flexleft flex_wrap">
				<view class="itemimgBox flexcenter margin_top2" v-for="(item,index) in images_text" :key="index">
					<!-- 已上传 -->
					<view class="listleftimg">
						<image class="listleftimg radius" :src="item" mode=""></image>
						<view class="deleteimg" @click="delindex(index,images,images_text)"></view>
					</view>
				</view>
				<view class="itemimgBox flexcenter margin_top2" @click="uploadImg(3,images,images_text)"
					v-if="images.length<3">
					<!-- 未上传 -->
					<image class="listleftimg radius" src="@/static/images/system/upimg.png" mode=""></image>
				</view>
			</view>
		</view>
		<view class="gudingdb mainpadding">
			<view class="bigbtn" @click="submitadd()" v-if="type==1">确认添加</view>
			<view class="bigbtn" @click="submitedit()" v-if="type==2">保存</view>
		</view>
	</view>
</template>
<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				images: [],
				images_text: [],
				name: "",
				type: 1, //1添加，2编辑
			}
		},
		onLoad(options) {
			this.listid = options.id
			this.type = options.type
			if (this.type == 2) {
				this.getDetail()
			}

		},
		methods: {
			getDetail() {
				httpRequest.request('/api/admintask/taskItemDetail', 'GET', {
					id: this.listid,
				}).then(res => {
					this.name = res.data.name
					this.images_text = res.data.images_text
					this.images = res.data.images.join()
				})
			},
			submitadd() { //添加
				if (this.name == "") {
					httpRequest.toast("请输入备注内容")
					return false
				}
				httpRequest.request('/api/admintask/addTaskItem', 'GET', {
					task_id: this.listid,
					name: this.name,
					images: this.images,
				}).then(res => {
					httpRequest.toast(res.msg)
					if (res.code == 1) {
						setTimeout(() => {
							uni.navigateBack(1)
						}, 1000)
					}
				})
			},
			submitedit() { //编辑
				if (this.name == "") {
					httpRequest.toast("请输入备注内容")
					return false
				}
				httpRequest.request('/api/admintask/editTaskItem', 'GET', {
					id: this.listid,
					name: this.name,
					images: this.images,
				}).then(res => {
					httpRequest.toast(res.msg)
					if (res.code == 1) {
						setTimeout(() => {
							uni.navigateBack(1)
						}, 1000)
					}
				})
			},
			uploadImg(value, upImgs, upImgs_text) { //上传图片
				let _this = this;
				let num = Number(value) - upImgs.length;
				if (num <= 0) {
					return false;
				}
				httpRequest.chooseImage(num, ['camera']).then(res => {
					for (var i = 0; i < res.length; i++) {
						_this.uploadImageService(res[i], upImgs, upImgs_text)
					}
				}).catch(err => {});
			},
			delindex(index, upImg, upImgs_text) {
				upImg.splice(index, 1)
				upImgs_text.splice(index, 1)
			},
			uploadImageService(url, upImg, upImgs_text) {
				let _this = this;
				httpRequest.uploadFile('/api/common/upload', url).then(res => {
					let image_item = res.fullurl;
					upImg.push(res.url)
					upImgs_text.push(image_item);
				}).catch(err => {});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.sanchu {
		position: absolute;
		top: 0;
		right: 0;
		width: 25rpx;
		height: 25rpx;
	}

	.scbox {
		width: 187rpx;
		height: 187rpx;
	}

	.textarea {
		box-sizing: border-box;
		padding: 30rpx;
		width: 100%;
		background: #FFFFFF;
		border-radius: 16rpx 16rpx 16rpx 16rpx;
	}
	.listleftimg {
		width: 160rpx;
		height: 160rpx;
		min-width: 160rpx;
		position: relative;
	}
</style>